/**
 * Created by HTWX on 2018/3/16.
 */
var express = require('express');
var router = express.Router();
var BaseModel = require('../models/base_model');
var baseModel = new BaseModel();
var CheckLogin = require('../models/checkLogin');
var checkLogin =new CheckLogin();
var Q=require('q');

var mark;
var body;
var select;
var papercount;

/* GET users listing. */
router.get('/', function(req, res,next) {
    papercount=req.cookies.papercount;
    if(req.cookies.mark.indexOf("|" + req.query.num + ".")!=-1){
        mark='取消标疑';
    }else{
        mark='标疑';
    }
    next();
});

router.get('/', function(req, res,next) {
    var sql = "select bodydetail from yk_question,yk_paperbody where yk_question.paper_id=" + req.cookies.paper_id + " and num=" + req.query.num + " and yk_question.body_id=yk_paperbody.body_id";
    baseModel.findOneById(sql, function (ret) {
        if (ret) {
            body = ret.bodydetail;
        } else {
            body = null;
        }
        next();
    });
});

router.get('/', function(req, res,next) {
        var list;
        var option;
        select = "";
        var cedit = req.cookies.report;
        if (cedit != "")
        {
            list = cedit.split('|');
            for (var i = 1; i < list.length; i++)
            {
                option = list[i].split('.');
                if ("|" + option[0] + "." == "|" + req.query.num + ".")
                {
                    select = option[1];
                }
            }
        }
    next();
});

function init(data,req){
    var deferred = Q.defer();
    var sql = "select question,answer,answerscore,num,yk_questionmark.questionmark,questionstatus_id,image_path,options,body_id from yk_question,yk_questionmark where paper_id=" + req.cookies.paper_id + " and num=" + data + " and yk_question.mark_id=yk_questionmark.mark_id";
    baseModel.findOneById(sql,function(ret){
        if(ret){
            deferred.resolve(ret);
        }else{
            deferred.resolve(null);
        }
    });
    return deferred.promise;
}

router.get('/', function(req, res) {
    res.cookie('num',req.query.num, {maxAge:100000*6000, path:'/', httpOnly:false});
    init(req.query.num,req).then(function(ret){
        if(ret){
            res.render('testinfo', {
                title: '医药考试',
                question:ret.question,
                answer:ret.answer,
                answerscore:ret.answerscore,
                num:ret.num,
                questionmark:ret.questionmark,
                questionstatus_id:ret.questionstatus_id,
                image_path:ret.image_path,
                option:ret.options,
                body_id:ret.body_id,
                body:body,
                mark:mark,
                select:select,
                papercount:papercount
            });
        }else{
            render('testinfo', {
                title: '医药考试'});
        }
    });
});

module.exports = router;

